<?php

// @formatter:off
/**
 * @file image.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: image.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/readrate/get/image.php $
 */
// @formatter:on

class WCServiceReadrateGetImage extends WCService {
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_requiredParams["url"][] = "image";
		$this->_requiredParams["url"][] = "group";

		$this->_requiredPermissions["to"][] = WC_PERM_IMAGE_SET_RATE;
		$this->_requiredPermissions["to"][] = WC_PERM_IMAGE_SET_READ;

		$this->_useCache = false;
	}
	//
	// Public methods.
	public function check() {
		if(parent::check()) {
			global $wcImage;
			global $wcGroup;
			global $wcUser;

			if(!$wcGroup || !$wcGroup->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified group id");
			}
			if(!$wcImage || !$wcImage->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified image id");
			}
			if($this->ok() && $wcGroup->id() != $wcImage->group) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "Unmatching groups");
			}
		}

		return $this->ok();
	}
	//
	// Protected methods.
	protected function runGET() {
		global $wcImage;
		global $wcUser;

		$rrManager = WCReadRate::I();

		$this->_results = array(
			"personal" => array(),
			"global" => array()
		);

		if($wcUser && $wcUser->ok()) {
			$rates = $rrManager->userImageReadRate($wcImage, $wcUser);
			$this->_results["personal"]["read"] = $rates["read"]["read"];
			$this->_results["personal"]["rate"] = $rates["rate"]["rate"];
			$this->_results["personal"]["pending"] = trim($rates["rate"]["date"]) == "";
		}

		$rates = $rrManager->imageReadRate($wcImage);
		$this->_results["global"]["reads"] = $rates["reads"];
		$this->_results["global"]["rate"] = $rates["rate"];
		$this->_results["global"]["votes"] = $rates["votes"];
	}
}
